package com.yyj.web.controller;

import com.google.common.collect.Maps;
import com.yyj.common.BaseResponse;
import com.yyj.constant.ReturnCode;
import com.yyj.constant.WebConstant;
import com.yyj.enums.PaymentTypeEnum;
import com.yyj.enums.RepairRecordStatusEnum;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.service.RepairRecordService;
import com.yyj.util.RspUtils;
import com.yyj.util.security.MD5Util;
import com.yyj.web.protocol.PageRequest;
import com.yyj.web.protocol.req.RepairRecordQueryReq;
import com.yyj.web.protocol.req.RepairRecordReq;
import com.yyj.web.protocol.req.RepairReplyReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Api(value = "报修相关接口" , description="报修相关接口")
@RestController
public class RepairRequireController extends BaseController{

    @Autowired
    RepairRecordService repairRecordService;

    @ApiOperation(value = "用户提交故障报修")
    @RequestMapping(value = "/auth/repair/submit" , method = RequestMethod.POST)
    public BaseResponse submitRepairRecord(@RequestBody @Validated RepairRecordReq repairRecordReq , BindingResult result){

        try{
            checkRequestParams(result);
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            repairRecordReq.setRoomNum(sessionInfo.getRoomNum());
            repairRecordReq.setMobile(sessionInfo.getMobile());
            repairRecordService.submitRepairRecord(repairRecordReq);
            return RspUtils.success("故障报修成功！");

        }catch(Exception e){
            logger.error("故障报修异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }

    }

    @ApiOperation(value = "业主查询故障报修记录列表")
    @RequestMapping(value = "/auth/owners/repairs" , method = RequestMethod.GET)
    public BaseResponse queryRepairRecordList(PageRequest pageRequest){
        try{
            RepairRecordQueryReq repairRecordQueryReq = new RepairRecordQueryReq();
            repairRecordQueryReq.setCurrentPage(pageRequest.getCurrentPage());
            repairRecordQueryReq.setPageSize(pageRequest.getPageSize());
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            repairRecordQueryReq.setRoomNum(sessionInfo.getRoomNum());
            return RspUtils.success(repairRecordService.queryRepairRecords(repairRecordQueryReq));

        }catch(Exception e){
            logger.error("查询故障记录异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }

    }


    @ApiOperation(value = "业主查询故障报修记录详情")
    @RequestMapping(value = "/auth/owners/repairs/{id}" , method = RequestMethod.GET)
    public BaseResponse queryRepairRecordDetail(@PathVariable Long id){
        try{
            return RspUtils.success(repairRecordService.queryRepairRecordResp(id));

        }catch(Exception e){
            logger.error("查询故障记录异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }
    }

    @ApiOperation(value = "业主回复管理员故障报修跟进说明")
    @RequestMapping(value = "/auth/owners/repairs/reply" , method = RequestMethod.POST)
    public BaseResponse reply(@RequestBody @Validated RepairReplyReq repairReplyReq , BindingResult result){
        try{
            checkRequestParams(result);
            return RspUtils.success(repairRecordService.ownerReply(repairReplyReq));
        }catch(Exception e){
            logger.error("业主回复管理员故障报修跟进说明异常：{}" , e.getMessage() , e);
            return RspUtils.error(ReturnCode.NORMAL_ERROR , e.getMessage());
        }
    }

    @ApiOperation(value = "维修状态类型说明")
    @RequestMapping(value = "/repairs/type", method = RequestMethod.GET)
    public BaseResponse paymentType(
    ){
        try{
            HashMap<String , String> typeMap = Maps.newHashMap();
            for(RepairRecordStatusEnum repairRecordStatusEnum : RepairRecordStatusEnum.values()){
                typeMap.put(repairRecordStatusEnum.getCode() , repairRecordStatusEnum.getDesc());
            }
            return RspUtils.success(typeMap);
        }catch(Exception e){
            logger.error("业主生活缴费异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.MD5("123456"));
    }


}
